﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ErrorLog : System.Web.UI.Page
{
    public int AddOrUpdateSign = 0;

    public string SendMessage = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            if (Session["TestList"] == null)
            {

                DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();
                this.GridView1.DataSource = errorlogdals.GetAll();
                string[] keys ={ "ErrorLogId" };
                GridView1.DataKeyNames = keys;
                GridView1.DataBind();

                DAL.DomainDALs domaindals = new DAL.DomainDALs();
                this.DDLDomain.DataSource = domaindals.GetAll();
                this.DDLDomain.DataBind();

                DDLDomain.Items.Insert(0, new ListItem("请选择域名", "0"));
                DDLUrl.Items.Insert(0, new ListItem("请选择URL", "0"));

               
            }
            else
            {
                this.GridView1.DataSource = Session["TestList"];
                string[] keys ={ "ErrorLogId" };
                GridView1.DataKeyNames = keys;
                GridView1.DataBind();
                Session.Remove("TestList");

                DAL.DomainDALs domaindals = new DAL.DomainDALs();
                this.DDLDomain.DataSource = domaindals.GetAll();
                this.DDLDomain.DataBind();
                DDLDomain.Items.Insert(0, new ListItem("请选择域名", "0"));
                DDLUrl.Items.Insert(0, new ListItem("请选择URL", "0"));
            }
        }

        
    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       
        GridView1.PageIndex = e.NewPageIndex;
        bind();
        //DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();    
        //this.GridView1.DataSource = errorlogdals.GetAll();
        //string[] keys ={ "ErrorLogId" };
        //GridView1.DataKeyNames = keys;
        //GridView1.DataBind(); 
    }


    public string GetSendMessage(int ErrorLogId)
    {
        DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();
        Entities.ErrorLog errolog = errorlogdals.GetByID(ErrorLogId);
        DAL.UrlDALs urldals = new DAL.UrlDALs();
        Entities.Url url = urldals.GetByID(errolog.UrlID);
        return url.SendMessage; 
    }

    public string GetDomain(int ErrorLogId)
    {
        DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();
        Entities.ErrorLog errolog = errorlogdals.GetByID(ErrorLogId);
        DAL.DomainDALs domaindals = new DAL.DomainDALs();
        Entities.Domain domain = domaindals.GetByID(errolog.DomainID);
        return domain.Domain;
    }

    public string GetURL(int ErrorLogId)
    {
      
            DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();
            Entities.ErrorLog errolog = errorlogdals.GetByID(ErrorLogId);
            DAL.UrlDALs urldals = new DAL.UrlDALs();
            Entities.Url url = urldals.GetByID(errolog.UrlID);
            if (url != null)
                return url.Url;
            else
                return string.Empty;
      
    }

    public string GetPrevValue(int ErrorLogId,string content)
    {
        DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();
        Entities.ErrorLog errolog = errorlogdals.GetByID(ErrorLogId);
        DAL.QueryDALs querydals = new DAL.QueryDALs();
        Entities.Query query = querydals.GetByID(errolog.QueryID);
        string right = "";
        if (query != null)
        {
            right = query.Right;

            if (query.Type == "1")
            {
                int intright = int.Parse(right) * 1000;
                right = intright.ToString();
            }

            return content + ",预设值是: " + right;
        }
        else
            return content;
    }

    public string GetLink(int ErrorLogId)
    {
        string domain = this.GetDomain(ErrorLogId);
        string url = this.GetURL(ErrorLogId);

        return domain + url;
    }

    protected void DDLDomain_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLDomain.SelectedItem.Text == "请选择域名")
        {
            DDLUrl.Items.Clear();
            DDLUrl.Items.Insert(DDLDomain.SelectedIndex, new ListItem("请选择URL", "0"));
        }
        else
        {
            int id = int.Parse(this.DDLDomain.SelectedItem.Value);
            DAL.UrlDALs urldals = new DAL.UrlDALs();

            if (urldals.GetUrlByDomainId(id).Count != 0)
            {
                this.DDLUrl.DataSource = urldals.GetUrlByDomainId(id);
                this.DDLUrl.DataBind();
                DDLUrl.Items.Insert(0, new ListItem("请选择URL", "0"));
            }
            else
            {
                DDLUrl.Items.Clear();
                DDLUrl.Items.Insert(0, new ListItem("请选择URL", "0"));
            }
        }
    }
    private void bind()
    {
        DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();

        if (DDLDomain.SelectedValue == "0" && DDLUrl.SelectedValue != "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(null, int.Parse(DDLUrl.SelectedValue));
        else if (DDLDomain.SelectedValue != "0" && DDLUrl.SelectedValue == "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(int.Parse(DDLDomain.SelectedValue), null);
        else if (DDLDomain.SelectedValue != "0" && DDLUrl.SelectedValue != "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(int.Parse(DDLDomain.SelectedValue), int.Parse(DDLUrl.SelectedValue));
        else if (DDLDomain.SelectedValue == "0" && DDLUrl.SelectedValue == "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(null, null);

        string[] keys ={ "ErrorLogId" };
        GridView1.DataKeyNames = keys;
        GridView1.DataBind();
    }
    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        DAL.ErrorLogDALs errorlogdals = new DAL.ErrorLogDALs();

        if (DDLDomain.SelectedValue == "0" && DDLUrl.SelectedValue != "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(null, int.Parse(DDLUrl.SelectedValue));
        else if (DDLDomain.SelectedValue != "0" && DDLUrl.SelectedValue == "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(int.Parse(DDLDomain.SelectedValue),null);
        else if (DDLDomain.SelectedValue != "0" && DDLUrl.SelectedValue != "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(int.Parse(DDLDomain.SelectedValue), int.Parse(DDLUrl.SelectedValue));
        else if (DDLDomain.SelectedValue == "0" && DDLUrl.SelectedValue == "0")
            this.GridView1.DataSource = errorlogdals.GetUrlByDomainId(null,null);

        string[] keys ={ "ErrorLogId" };
        GridView1.DataKeyNames = keys;
        GridView1.DataBind();

        DAL.DomainDALs domaindals = new DAL.DomainDALs();
        //this.DDLDomain.DataSource = domaindals.GetAll();
        //this.DDLDomain.DataBind();
        
        Session["domainselectindex"] = DDLDomain.SelectedIndex;
        Session["urlselectindex"] = DDLUrl.SelectedIndex;

    }
    public string ConvertString(string value)
    {
        if (value.Length > 10)
            return value.Substring(0, 10) + "...";
        else
            return value;
    }
    //protected void GridView1_DataBound(object sender, EventArgs e)
    //{
    //    for (int i = 0; i < GridView1.Rows.Count; i++)
    //    {
    //        GridView1.Rows[i].Cells[4].ToolTip = GridView1.Rows[i].Cells[4].Text;
    //        if (GridView1.Rows[i].Cells[4].Text.Length > 15)
    //            GridView1.Rows[i].Cells[4].Text = GridView1.Rows[i].Cells[4].Text.Substring(0, 15) + "...";
    //    }
    //}

    protected void DDLUrl_DataBound(object sender, EventArgs e)
    {
        
    }
}
